package topin.facecat.FaceCat.Service

import facecat.topin.core.FCTran
import topin.facecat.FaceCat.UI.ClientTickDataCache
import java.util.Calendar
import java.util.GregorianCalendar

/*
 * 股票服务
 */
object StockService {
    /*
     * 获取指定年月日的星期
     *@param year 年
     *@param month 月
     *@param day 日
     */
    fun dayOfWeek(y: Int, m: Int, d: Int): Int {
        var y = y
        var m = m
        if (m == 1 || m == 2) {
            m += 12
            y--
        }
        return (d + 2 * m + 3 * (m + 1) / 5 + y + y / 4 - y / 100 + y / 400) % 7
    }

    /*
     * 获取季度
     */
    fun getSeason(month: Int): Int {
        return if (month >= 1 && month <= 3) {
            1
        } else if (month >= 4 && month <= 6) {
            2
        } else if (month >= 7 && month <= 9) {
            3
        } else {
            4
        }
    }

    /*
     * 合并最新数据
     *@param oldDatas 老数据
     *@param latestData 新数据
     *@param tickDataCache TICK数据
     *@param cycle 周期
     */
    fun mergeLatestData(
        code: String?,
        oldDatas: ArrayList<SecurityData>,
        latestData: SecurityData,
        tickDataCache: ClientTickDataCache,
        dCycle: Int
    ) {
        var cycle = dCycle
        if (cycle == 0) {
            cycle = 1
        }
        if (latestData.m_open <= 0 || latestData.m_volume <= 0 || latestData.m_amount <= 0) {
            return
        }
        //        DateTime newDate = FCTran.numToDate(latestData.m_date);
        var newDate = FCTran.numToDate(latestData.m_date)
        if (newDate[Calendar.HOUR_OF_DAY] * 60 + newDate[Calendar.MINUTE] < 570) {
            newDate = GregorianCalendar(
                newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                newDate[Calendar.DAY_OF_MONTH], 9, 30, 0
            )
            latestData.m_date = FCTran.dateToNum(newDate)
        } else if (newDate[Calendar.HOUR_OF_DAY] * 60 + newDate[Calendar.MINUTE] < 571) {
            newDate = GregorianCalendar(
                newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                newDate[Calendar.DAY_OF_MONTH], 9, 31, 0
            )
            latestData.m_date = FCTran.dateToNum(newDate)
        } else if (newDate[Calendar.HOUR_OF_DAY] * 60 + newDate[Calendar.MINUTE] > 900) {
            newDate = GregorianCalendar(
                newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                newDate[Calendar.DAY_OF_MONTH], 15, 0, 0
            )
            latestData.m_date = FCTran.dateToNum(newDate)
        } else if (newDate[Calendar.HOUR_OF_DAY] * 60 + newDate[Calendar.MINUTE] > 690 && newDate[Calendar.HOUR_OF_DAY] * 60 + newDate[Calendar.MINUTE] < 780) {
            newDate = GregorianCalendar(
                newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                newDate[Calendar.DAY_OF_MONTH], 11, 30, 0
            )
            latestData.m_date = FCTran.dateToNum(newDate)
        }
        var isNextCycle = true
        if (dCycle == 0) {
            isNextCycle = false
        } else if (cycle < 1440) {
            if (oldDatas.size > 0) {
                val newMinutes = (latestData.m_date / 60).toInt()
                val lastMinutes = (oldDatas[oldDatas.size - 1].m_date / 60).toInt()
                isNextCycle = newMinutes - lastMinutes >= cycle
            }
        } else {
            if (cycle == 1440) {
                if (oldDatas.size > 0) {
                    val lastDate = FCTran.numToDate(oldDatas[oldDatas.size - 1].m_date)
                    isNextCycle = (FCTran.dateToNum(
                        GregorianCalendar(
                            newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                            newDate[Calendar.DAY_OF_MONTH]
                        )
                    )
                            != FCTran.dateToNum(
                        GregorianCalendar(
                            lastDate[Calendar.YEAR], lastDate[Calendar.MONTH] + 1,
                            lastDate[Calendar.DAY_OF_MONTH]
                        )
                    ))
                }
            } else if (cycle == 10080) {
                if (oldDatas.size > 0) {
                    val firstDate = FCTran.getDateNum(1970, 1, 5, 0, 0, 0, 0)
                    val lWeeks =
                        ((oldDatas[oldDatas.size - 1].m_date - firstDate) / 86400).toInt() / 7
                    val weeks = ((latestData.m_date - firstDate) / 86400).toInt() / 7
                    isNextCycle = if (weeks > lWeeks) {
                        true
                    } else {
                        false
                    }
                }
            } else if (cycle == 43200) {
                if (oldDatas.size > 0) {
                    val lastDate = FCTran.numToDate(oldDatas[oldDatas.size - 1].m_date)
                    isNextCycle =
                        newDate[Calendar.YEAR] * 12 + newDate[Calendar.MONTH] + 1 != lastDate[Calendar.YEAR] * 12 + lastDate[Calendar.MONTH] + 1
                }
            } else if (cycle == 129600) {
                if (oldDatas.size > 0) {
                    val lastDate = FCTran.numToDate(oldDatas[oldDatas.size - 1].m_date)
                    isNextCycle = newDate[Calendar.YEAR] * 4 + getSeason(
                        newDate[Calendar.MONTH] + 1
                    ) != lastDate[Calendar.YEAR] * 4 + getSeason(
                        lastDate[Calendar.MONTH] + 1
                    )
                }
            } else if (cycle == 259200) {
                if (oldDatas.size > 0) {
                    val lastDate = FCTran.numToDate(oldDatas[oldDatas.size - 1].m_date)
                    isNextCycle =
                        newDate[Calendar.YEAR] * 2 + (newDate[Calendar.MONTH] + 1 - 1) / 6 != lastDate[Calendar.YEAR] * 2 + (lastDate[Calendar.MONTH] + 1 - 1) / 6
                }
            } else if (cycle == 518400) {
                if (oldDatas.size > 0) {
                    val lastDate = FCTran.numToDate(oldDatas[oldDatas.size - 1].m_date)
                    isNextCycle = newDate[Calendar.YEAR] != lastDate[Calendar.YEAR]
                }
            }
        }
        if (isNextCycle) {
            val newCycleData = SecurityData()
            val lastDate = FCTran.numToDate(latestData.m_date)
            if (cycle < 1440) {
                newCycleData.m_date = FCTran.getDateNum(
                    lastDate[Calendar.YEAR], lastDate[Calendar.MONTH] + 1,
                    lastDate[Calendar.DAY_OF_MONTH],
                    lastDate[Calendar.HOUR_OF_DAY],
                    lastDate[Calendar.MINUTE], 0, 0
                )
            } else {
                newCycleData.m_date = FCTran.getDateNum(
                    lastDate[Calendar.YEAR], lastDate[Calendar.MONTH] + 1,
                    lastDate[Calendar.DAY_OF_MONTH], 0, 0, 0, 0
                )
            }
            newCycleData.m_close = latestData.m_close
            newCycleData.m_high = latestData.m_close
            newCycleData.m_low = latestData.m_close
            newCycleData.m_open = latestData.m_close
            newCycleData.m_volume = latestData.m_volume - tickDataCache.m_lastVolume
            newCycleData.m_amount = latestData.m_amount - tickDataCache.m_lastAmount
            oldDatas.add(newCycleData)
        } else {
            if (oldDatas.size > 0) {
                var lastCycleData = oldDatas[oldDatas.size - 1]
                if (dCycle == 0) {
                    val newData = FCTran.getDateNum(
                        newDate[Calendar.YEAR], newDate[Calendar.MONTH] + 1,
                        newDate[Calendar.DAY_OF_MONTH],
                        newDate[Calendar.HOUR_OF_DAY],
                        newDate[Calendar.MINUTE], 0, 0
                    )
                    for (i in oldDatas.indices) {
                        val oData = oldDatas[i]
                        if (oData.m_date == newData) {
                            oData.m_open = latestData.m_open
                            lastCycleData = oData
                            break
                        }
                    }
                }
                lastCycleData.m_close = latestData.m_close
                if (lastCycleData.m_high < latestData.m_close) {
                    lastCycleData.m_high = latestData.m_close
                }
                if (lastCycleData.m_low > lastCycleData.m_close) {
                    lastCycleData.m_low = latestData.m_close
                }
                lastCycleData.m_amount += latestData.m_amount - tickDataCache.m_lastAmount
                lastCycleData.m_volume += latestData.m_volume - tickDataCache.m_lastVolume
            }
        }
        tickDataCache.m_code = code!!
        tickDataCache.m_lastAmount = latestData.m_amount
        tickDataCache.m_lastDate = latestData.m_date
        tickDataCache.m_lastVolume = latestData.m_volume
    }

    var m_factors = HashMap<String, ArrayList<ADJUSTMENTFACTOR>>()
    fun fq_price_func(price: Double, factor: ADJUSTMENTFACTOR): Double {
        val cash_bt = factor.f1
        val bonus_shr = factor.f3
        val allot_pct = factor.f4
        val allot_price = factor.f2
        return (10.0 * price - cash_bt + allot_pct * allot_price) / (10.0 + allot_pct + bonus_shr)
    }

    fun fq_price_func2(price: Double, factor: ADJUSTMENTFACTOR): Double {
        val cash_bt = factor.f1
        val bonus_shr = factor.f3
        val allot_pct = factor.f4
        val allot_price = factor.f2
        return (price * (10.0 + allot_pct + bonus_shr) - allot_pct * allot_price + cash_bt) / 10.0
    }

    /*
     * 转换前复权
     *@param code
     *@param kd
     */
    fun ConvertXDRBeforePrice(
        code: String?,
        kd: SecurityData,
        trade_date: Double,
        factor: ArrayList<ADJUSTMENTFACTOR>
    ) {
        val size = factor.size
        if (size > 0) {
            var pos = 0
            val date = kd.m_date
            if (kd.m_date < factor[factor.size - 1].dwDate) {
                for (i in 0 until size) {
                    if (trade_date > 0 && trade_date < factor[i].dwDate) {
                        continue
                    }
                    pos = i
                    if (date < factor[i].dwDate) {
                        break
                    }
                }
                for (i in pos until size) {
                    if (trade_date > 0 && trade_date < factor[i].dwDate) {
                        continue
                    }
                    kd.m_open = fq_price_func(kd.m_open, factor[i])
                    kd.m_high = fq_price_func(kd.m_high, factor[i])
                    kd.m_low = fq_price_func(kd.m_low, factor[i])
                    kd.m_close = fq_price_func(kd.m_close, factor[i])
                }
            }
        }
    }

    /*
     * 转换后复权
     *@param code
     *@param kd
     */
    fun ConvertXDRAfterPrice(
        code: String?,
        kd: SecurityData,
        trade_date: Double,
        factor: ArrayList<ADJUSTMENTFACTOR>
    ) {
        val size = factor.size
        if (size > 0) {
            val date = kd.m_date
            val factors = ArrayList<ADJUSTMENTFACTOR>()
            for (i in 0 until size) {
                if (date < factor[i].dwDate) {
                    break
                } else {
                    factors.add(0, factor[i])
                }
            }
            for (i in factors.indices) {
                kd.m_open = fq_price_func2(kd.m_open, factors[i])
                kd.m_high = fq_price_func2(kd.m_high, factors[i])
                kd.m_low = fq_price_func2(kd.m_low, factors[i])
                kd.m_close = fq_price_func2(kd.m_close, factors[i])
            }
            factors.clear()
        }
    }

    fun ConvertXDR(code: String, rights_offering: Int, datas: ArrayList<SecurityData>) {
        if (m_factors.containsKey(code)) {
            val factor = m_factors[code]!!
            val datasSize = datas.size
            if (datasSize > 0) {
                val trade_date = datas[datas.size - 1].m_date
                // 复权K线
                for (i in 0 until datasSize) {
                    val kd = datas[i]
                    if (rights_offering == 1) {
                        ConvertXDRBeforePrice(code, kd, trade_date, factor)
                    } else if (rights_offering == 2) {
                        ConvertXDRAfterPrice(code, kd, trade_date, factor)
                    }
                }
            }
        }
    }

    fun multiMinuteSecurityDatas(
        newDatas: ArrayList<SecurityData>,
        minuteDatas: ArrayList<SecurityData>,
        cycle: Int
    ) {
        var lastMinutes = 0
        if (cycle > 0) {
//            newDatas.Capacity = minuteDatas.size() / cycle;
        }
        for (i in minuteDatas.indices) {
            val minuteData = minuteDatas[i]!!
            val minutes = (minuteData.m_date / 60).toInt()
            if (lastMinutes == 0) {
                lastMinutes = minutes
            }
            //更新
            if (newDatas.size > 0 && minutes - lastMinutes < cycle) {
                val lastData = newDatas[newDatas.size - 1]
                lastData.m_close = minuteData.m_close
                if (minuteData.m_high > lastData.m_high) {
                    lastData.m_high = minuteData.m_high
                }
                if (minuteData.m_low < lastData.m_low) {
                    lastData.m_low = minuteData.m_low
                }
                lastData.m_amount += minuteData.m_amount
                lastData.m_volume += minuteData.m_volume
                lastData.m_endDate = minuteData.m_date
            } else {
                val newData = SecurityData()
                newData.m_date = minuteData.m_date
                newData.m_high = minuteData.m_high
                newData.m_low = minuteData.m_low
                newData.m_open = minuteData.m_open
                newData.m_close = minuteData.m_close
                newData.m_amount = minuteData.m_amount
                newData.m_volume = minuteData.m_volume
                newData.m_startDate = minuteData.m_date
                newData.m_endDate = minuteData.m_date
                newDatas.add(newData)
                lastMinutes = minutes
            }
        }
    }

    fun getHistoryWeekDatas(
        weekDatas: ArrayList<SecurityData>,
        dayDatas: ArrayList<SecurityData>
    ): Int {
        val dayDatasSize = dayDatas.size
        //        weekDatas.Capacity = dayDatas.size() / 4;
        if (dayDatasSize > 0) {
            val firstDate = FCTran.getDateNum(1970, 1, 5, 0, 0, 0, 0)
            var weekData = SecurityData()
            weekData.copy(dayDatas[0]!!)
            weekData.m_startDate = weekData.m_date
            var lWeeks = ((weekData.m_date - firstDate) / 86400 / 7).toInt()
            for (i in 0 until dayDatasSize) {
                val dayData = SecurityData()
                dayData.copy(dayDatas[i]!!)
                val weeks = ((dayData.m_date - firstDate) / 86400 / 7).toInt()
                var isNextWeek = false
                if (weeks > lWeeks) {
                    isNextWeek = true
                }
                if (isNextWeek) {
                    weekDatas.add(weekData)
                    weekData = dayData
                    weekData.m_startDate = weekData.m_date
                    if (i == dayDatasSize - 1) {
                        weekData.m_endDate = dayData.m_date
                        weekDatas.add(weekData)
                    }
                } else {
                    if (i > 0) {
                        weekData.m_endDate = dayData.m_date
                        weekData.m_close = dayData.m_close
                        weekData.m_amount += dayData.m_amount
                        weekData.m_volume += dayData.m_volume
                        if (weekData.m_high < dayData.m_high) {
                            weekData.m_high = dayData.m_high
                        }
                        if (weekData.m_low > dayData.m_low) {
                            weekData.m_low = dayData.m_low
                        }
                    }
                    if (i == dayDatasSize - 1) {
                        weekData.m_endDate = dayData.m_date
                        weekDatas.add(weekData)
                    }
                }
                lWeeks = weeks
            }
        }
        return 1
    }

    fun getHistoryMonthDatas(
        monthDatas: ArrayList<SecurityData>,
        dayDatas: ArrayList<SecurityData>
    ): Int {
        val dayDatasSize = dayDatas.size
        if (dayDatasSize > 0) {
//            monthDatas.Capacity = dayDatas.size() / 20;
            var monthData = SecurityData()
            monthData.copy(dayDatas[0]!!)
            monthData.m_startDate = monthData.m_date
            val ldate = FCTran.numToDate(monthData.m_date)
            var lYear = ldate[Calendar.YEAR]
            var lMonth = ldate[Calendar.MONTH] + 1
            var lDay = ldate[Calendar.DAY_OF_MONTH]
            val lHour = ldate[Calendar.HOUR_OF_DAY]
            val lMinute = ldate[Calendar.MINUTE]
            val lSecond = ldate[Calendar.SECOND]
            val lMs = ldate[Calendar.MILLISECOND]
            //            FCTran.getDateByNum(monthData.m_date, ReflYear, ReflMonth, ReflDay, ReflHour, ReflMinute, ReflSecond, ReflMs);
            for (i in 0 until dayDatasSize) {
                val dayData = SecurityData()
                dayData.copy(dayDatas[i]!!)
                val date = FCTran.numToDate(dayData.m_date)
                val year = date[Calendar.YEAR]
                val month = date[Calendar.MONTH] + 1
                val day = date[Calendar.DAY_OF_MONTH]
                val hour = date[Calendar.HOUR_OF_DAY]
                val minute = date[Calendar.MINUTE]
                val second = date[Calendar.SECOND]
                val ms = date[Calendar.MILLISECOND]
                val isNextMonth = year * 12 + month > lYear * 12 + lMonth
                if (isNextMonth) {
                    monthDatas.add(monthData)
                    monthData = dayData
                    monthData.m_startDate = monthData.m_date
                    if (i == dayDatasSize - 1) {
                        monthData.m_endDate = dayData.m_date
                        monthDatas.add(monthData)
                    }
                } else {
                    if (i > 0) {
                        monthData.m_endDate = dayData.m_date
                        monthData.m_close = dayData.m_close
                        monthData.m_amount += dayData.m_amount
                        monthData.m_volume += dayData.m_volume
                        if (monthData.m_high < dayData.m_high) {
                            monthData.m_high = dayData.m_high
                        }
                        if (monthData.m_low > dayData.m_low) {
                            monthData.m_low = dayData.m_low
                        }
                    }
                    if (i == dayDatasSize - 1) {
                        monthData.m_endDate = dayData.m_date
                        monthDatas.add(monthData)
                    }
                }
                lYear = year
                lMonth = month
                lDay = day
            }
        }
        return 1
    }

    fun getHistorySeasonDatas(
        seasonDatas: ArrayList<SecurityData>,
        dayDatas: ArrayList<SecurityData>
    ): Int {
        val dayDatasSize = dayDatas.size
        if (dayDatasSize > 0) {
//            seasonDatas.Capacity = dayDatas.size() / 60;
            var seaonData = SecurityData()
            seaonData.copy(dayDatas[0]!!)
            seaonData.m_startDate = seaonData.m_date
            val ldate = FCTran.numToDate(seaonData.m_date)
            var lYear = ldate[Calendar.YEAR]
            var lMonth = ldate[Calendar.MONTH] + 1
            var lDay = ldate[Calendar.DAY_OF_MONTH]
            val lHour = ldate[Calendar.HOUR_OF_DAY]
            val lMinute = ldate[Calendar.MINUTE]
            val lSecond = ldate[Calendar.SECOND]
            val lMs = ldate[Calendar.MILLISECOND]
            for (i in 0 until dayDatasSize) {
                val dayData = SecurityData()
                dayData.copy(dayDatas[i]!!)
                val date = FCTran.numToDate(dayData.m_date)
                val year = date[Calendar.YEAR]
                val month = date[Calendar.MONTH] + 1
                val day = date[Calendar.DAY_OF_MONTH]
                val hour = date[Calendar.HOUR_OF_DAY]
                val minute = date[Calendar.MINUTE]
                val second = date[Calendar.SECOND]
                val ms = date[Calendar.MILLISECOND]
                val isNextSeason = year * 4 + getSeason(month) > lYear * 4 + getSeason(lMonth)
                if (isNextSeason) {
                    seasonDatas.add(seaonData)
                    seaonData = dayData
                    seaonData.m_startDate = seaonData.m_date
                    if (i == dayDatasSize - 1) {
                        seaonData.m_endDate = dayData.m_date
                        seasonDatas.add(seaonData)
                    }
                } else {
                    if (i > 0) {
                        seaonData.m_endDate = dayData.m_date
                        seaonData.m_close = dayData.m_close
                        seaonData.m_amount += dayData.m_amount
                        seaonData.m_volume += dayData.m_volume
                        if (seaonData.m_high < dayData.m_high) {
                            seaonData.m_high = dayData.m_high
                        }
                        if (seaonData.m_low > dayData.m_low) {
                            seaonData.m_low = dayData.m_low
                        }
                    }
                    if (i == dayDatasSize - 1) {
                        seaonData.m_endDate = dayData.m_date
                        seasonDatas.add(seaonData)
                    }
                }
                lYear = year
                lMonth = month
                lDay = day
            }
        }
        return 1
    }

    fun getHistoryHalfYearDatas(
        halfYearDatas: ArrayList<SecurityData>,
        dayDatas: ArrayList<SecurityData>
    ): Int {
        val dayDatasSize = dayDatas.size
        if (dayDatasSize > 0) {
//            halfYearDatas.Capacity = dayDatas.size() / 120;
            var yearData = SecurityData()
            yearData.copy(dayDatas[0]!!)
            yearData.m_startDate = yearData.m_date
            val ldate = FCTran.numToDate(yearData.m_date)
            var lyear = ldate[Calendar.YEAR]
            var lmonth = ldate[Calendar.MONTH] + 1
            var lday = ldate[Calendar.DAY_OF_MONTH]
            val lhour = ldate[Calendar.HOUR_OF_DAY]
            val lminute = ldate[Calendar.MINUTE]
            val lsecond = ldate[Calendar.SECOND]
            val lms = ldate[Calendar.MILLISECOND]
            for (i in 0 until dayDatasSize) {
                val dayData = SecurityData()
                dayData.copy(dayDatas[i]!!)
                val date = FCTran.numToDate(dayData.m_date)
                val year = date[Calendar.YEAR]
                val month = date[Calendar.MONTH] + 1
                val day = date[Calendar.DAY_OF_MONTH]
                val hour = date[Calendar.HOUR_OF_DAY]
                val minute = date[Calendar.MINUTE]
                val second = date[Calendar.SECOND]
                val ms = date[Calendar.MILLISECOND]
                val isNextHalfYear = year * 2 + (month - 1) / 6 > lyear * 2 + (lmonth - 1) / 6
                if (isNextHalfYear) {
                    halfYearDatas.add(yearData)
                    yearData = dayData
                    yearData.m_startDate = yearData.m_date
                    if (i == dayDatasSize - 1) {
                        yearData.m_endDate = dayData.m_date
                        halfYearDatas.add(yearData)
                    }
                } else {
                    if (i > 0) {
                        yearData.m_endDate = dayData.m_date
                        yearData.m_close = dayData.m_close
                        yearData.m_amount += dayData.m_amount
                        yearData.m_volume += dayData.m_volume
                        if (yearData.m_high < dayData.m_high) {
                            yearData.m_high = dayData.m_high
                        }
                        if (yearData.m_low > dayData.m_low) {
                            yearData.m_low = dayData.m_low
                        }
                    }
                    if (i == dayDatasSize - 1) {
                        yearData.m_endDate = dayData.m_date
                        halfYearDatas.add(yearData)
                    }
                }
                lyear = year
                lmonth = month
                lday = day
            }
        }
        return 1
    }

    fun getHistoryYearDatas(
        yearDatas: ArrayList<SecurityData>,
        dayDatas: ArrayList<SecurityData>
    ): Int {
        val dayDatasSize = dayDatas.size
        if (dayDatasSize > 0) {
//            yearDatas.Capacity = dayDatas.size() / 240;
            var yearData = SecurityData()
            yearData.copy(dayDatas[0]!!)
            yearData.m_startDate = yearData.m_date
            val ldate = FCTran.numToDate(yearData.m_date)
            var lyear = ldate[Calendar.YEAR]
            var lmonth = ldate[Calendar.MONTH] + 1
            var lday = ldate[Calendar.DAY_OF_MONTH]
            val lhour = ldate[Calendar.HOUR_OF_DAY]
            val lminute = ldate[Calendar.MINUTE]
            val lsecond = ldate[Calendar.SECOND]
            val lms = ldate[Calendar.MILLISECOND]
            for (i in 0 until dayDatasSize) {
                val dayData = SecurityData()
                dayData.copy(dayDatas[i]!!)
                val date = FCTran.numToDate(dayData.m_date)
                val year = date[Calendar.YEAR]
                val month = date[Calendar.MONTH] + 1
                val day = date[Calendar.DAY_OF_MONTH]
                val hour = date[Calendar.HOUR_OF_DAY]
                val minute = date[Calendar.MINUTE]
                val second = date[Calendar.SECOND]
                val ms = date[Calendar.MILLISECOND]
                val isNextYear = year > lyear
                if (isNextYear) {
                    yearDatas.add(yearData)
                    yearData = dayData
                    yearData.m_startDate = yearData.m_date
                    if (i == dayDatasSize - 1) {
                        yearData.m_endDate = dayData.m_date
                        yearDatas.add(yearData)
                    }
                } else {
                    if (i > 0) {
                        yearData.m_endDate = dayData.m_date
                        yearData.m_close = dayData.m_close
                        yearData.m_amount += dayData.m_amount
                        yearData.m_volume += dayData.m_volume
                        if (yearData.m_high < dayData.m_high) {
                            yearData.m_high = dayData.m_high
                        }
                        if (yearData.m_low > dayData.m_low) {
                            yearData.m_low = dayData.m_low
                        }
                    }
                    if (i == dayDatasSize - 1) {
                        yearData.m_endDate = dayData.m_date
                        yearDatas.add(yearData)
                    }
                }
                lyear = year
                lmonth = month
                lday = day
            }
        }
        return 1
    }
}

